import datetime



'''
日期时间格式转换

# 将时间戳转换为datetime对象
dt = datetime.datetime.fromtimestamp(timestamp)
'''

print('1-----------------------------------------------')
# 获取当前时间
now = datetime.datetime.now()
print(now)


print('2-----------------------------------------------')
# 获取年份
year = now.year
print(year)
# 获取月份
month = now.month
print(month)
# 获取日期
day = now.day
print(day)
# 获取小时
hour = now.hour
print(hour)
# 获取分钟
minute = now.minute
print(minute)
# 获取秒
second = now.second
print(second)
# 获取毫秒
millisecond = now.microsecond
print(millisecond)
print('3-----------------------------------------------')
# 日期计算
# 计算10天前的日期
ten_days_ago = now - datetime.timedelta(days=10)
print(ten_days_ago)

# 计算10小时后的日期
ten_hours_later = now + datetime.timedelta(hours=10)
print(ten_hours_later)
# 计算10分钟前的日期
ten_minutes_ago = now - datetime.timedelta(minutes=10)
print(ten_minutes_ago)
# 计算10秒后的日期
ten_seconds_later = now + datetime.timedelta(seconds=10)
print(ten_seconds_later)
# 计算10毫秒后的日期
ten_milliseconds_later = now + datetime.timedelta(milliseconds=10)
print(ten_milliseconds_later)
print('4-----------------------------------------------')
# 日期格式化
# 将datetime对象格式化为字符串
formatted_date = now.strftime('%Y-%m-%d %H:%M:%S')
print(formatted_date)
# 将字符串格式化为datetime对象
parsed_date = datetime.datetime.strptime('2023-04-10 12:30:00', '%Y-%m-%d %H:%M:%S')
print(parsed_date)
print('5-----------------------------------------------')
# 日期比较
# 比较两个日期的大小
date1 = datetime.datetime(2023, 4, 10)
date2 = datetime.datetime(2023, 4, 11)
if date1 < date2:
    print('date1 is earlier than date2')
elif date1 > date2:
    print('date1 is later than date2')
else:
    print('date1 is equal to date2')

print('6-----------------------------------------------')
# 日期计算
# 计算两个日期之间的天数差
date1 = datetime.datetime(2023, 4, 10)
date2 = datetime.datetime(2023, 4, 11)
delta = date2 - date1
print(delta.days)
# 计算两个日期之间的小时差
delta = date2 - date1
print(delta.seconds // 3600)
# 计算两个日期之间的分钟差
delta = date2 - date1
print(delta.seconds // 60)
# 计算两个日期之间的秒差
delta = date2 - date1
print(delta.seconds)
# 计算两个日期之间的毫秒差
delta = date2 - date1
print(delta.microseconds // 1000)
print('7-----------------------------------------------')
# 如何获取当前时间戳
timestamp = datetime.datetime.now().timestamp()
print(timestamp)
# 将时间戳转换为datetime对象
dt = datetime.datetime.fromtimestamp(timestamp)
print(dt)
# 将datetime对象转换为时间戳
timestamp = dt.timestamp()
print(timestamp)
print('8-----------------------------------------------')
# 一个月的第一天
first_day_of_month = now.replace(day=1)
print(first_day_of_month)
# 一个月的最后一天
last_day_of_month = (now.replace(day=28) + datetime.timedelta(days=4)).replace(day=1) - datetime.timedelta(days=1)
print(last_day_of_month)
# 一个月的天数
days_in_month = (last_day_of_month - first_day_of_month).days + 1
print(days_in_month)
# 一个月的星期几
weekday_of_first_day = first_day_of_month.weekday()
print(weekday_of_first_day)
# 一个月的星期数
weeks_in_month = (days_in_month + weekday_of_first_day) // 7
print(weeks_in_month)

print('9-----------------------------------------------')
# 
